<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 卡片区 -->
      <el-card class="box-card">
        <!-- 标签页 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first" class="tab-pane">
            <!-- 头部 -->
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              class="department-header"
            >
              <!-- 左侧 -->
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <!-- 右侧 -->
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 负责人 -->
                  <el-col>负责人</el-col>
                  <!-- 操作下拉菜单 -->
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="addDeptsFn('')"
                        >添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- 树形主体布局 -->
            <!-- default-expand-all 是否展开结构 -->
            <!-- data展示数据 -->
            <!-- props 配置选项 -->
            <el-tree
              :data="treeData"
              :default-expand-all="true"
              :props="defaultProps"
              @node-click="handleNodeClick"
            >
              <!-- 右侧 -->
              <template slot-scope="{ data }">
                <el-row
                  type="flex"
                  justify="space-between"
                  style="height: 50px; width: 100%"
                  align="middle"
                >
                  <!-- 左侧 -->
                  <el-col :span="20">{{ data.name }}</el-col>
                  <!-- 右侧 -->
                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <!-- 负责人 -->
                      <el-col>{{ data.manager }}</el-col>
                      <!-- 操作下拉菜单 -->
                      <!-- v-if 当前部门为一级部门，就不显示删除文字 -->
                      <el-col>
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item
                              @click.native="addDeptsFn(data.id)"
                            >添加部门</el-dropdown-item>
                            <el-dropdown-item
                              @click.native="editDeptsFn(data.id)"
                            >编辑部门</el-dropdown-item>
                            <el-dropdown-item
                              v-if="data && !data.children"
                              @click.native="delDeptFn(data.id)"
                            >删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>

        <!-- 新增部门的弹框 -->
        <!-- close-on-click-modal 是否可以通过点击 modal 关闭 Dialog -->
        <el-dialog
          :title="isEdit ? '编辑部门' : '添加部门'"
          :visible.sync="deptDialogShow"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <!-- 弹框组件（每次打开重新创建，所以使用v-if控制显示） -->
          <!--  -->
          <dept-dialog
            v-if="deptDialogShow"
            :parent-id="parentId"
            :is-edit="isEdit"
            :origin-list="originList"
            @updateDeptEvent="updateDeptFn"
            @cancelEvent="cancelFn"
          />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDepartments, deleDepartmentAPI } from '@/api/departments'
import { getTreeData } from '@/utils/getTreeData'
import DeptDialog from './deptDialog.vue'
export default {
  components: {
    DeptDialog
  },
  data() {
    return {
      activeName: 'first',
      treeData: [
        {
          name: '财务部',
          manager: '总裁办',
          children: [
            {
              name: '财务 1 部',
              manager: '老王'
            },
            {
              name: '财务 2 部',
              manager: '老李'
            }
          ]
        }
      ],
      // 渲染树形结构数据
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      deptDialogShow: false, // 新增部门的弹框显示
      parentId: '',
      isEdit: false, // 是否是编辑
      originList: [] // 精简后的组织架构信息

    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 获取所有的组织架构
    async getDepartments() {
      const res = await getDepartments()
      console.log(res)
      // 将所有组织架构信息进行精简 (箭头函数中如果函数体为对象形式，大括号省略，用小括号包裹对象即可)
      this.originList = res.data.depts.map(item => ({
        code: item.code,
        name: item.name,
        pid: item.pid,
        id: item.id
      }))
      // this.originList = res.data.depts
      // 将所有组织架构信息从扁平数据转换为树形结构
      this.treeData = getTreeData(res.data.depts)
    },

    handleNodeClick(data) {
      // console.log(data)
    },

    // 新增部门
    addDeptsFn(id) {
      // console.log(id)
      this.isEdit = false
      this.deptDialogShow = true
      this.parentId = id
    },

    // 编辑后更新部门
    updateDeptFn() {
      this.deptDialogShow = false
      this.getDepartments()
    },

    // 关闭弹框
    cancelFn() {
      this.deptDialogShow = false
    },

    // 编辑部门
    editDeptsFn(id) {
      this.isEdit = true
      this.parentId = id
      this.deptDialogShow = true
    },

    // 删除部门
    async delDeptFn(id) {
      // 确认框
      const confirmRes = await this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmRes === 'cancel') return this.$message('取消删除')
      // 调用接口，删除部门
      const res = await deleDepartmentAPI(id)
      console.log(res)
      // 提示消息
      this.$message.success(res.message)
      // 重新渲染部门列表
      this.getDepartments()
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
  .departments-tree {
    margin: 12px 0px;
  }
  .el-dialog {
    width: 800px;
  }
}
</style>
